/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.app.mockexam.special.user.web;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.mockexam.special.record.entity.AppMockexamSpecialRecord;
import com.jeesite.modules.app.mockexam.special.record.service.AppMockexamSpecialRecordService;
import com.jeesite.modules.app.mockexam.special.user.entity.TmockexamSpecialUser;
import com.jeesite.modules.app.mockexam.special.user.service.TmockexamSpecialUserService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;

/**
 * 专项练习记录表Controller
 * @author zpp
 * @version 2020-02-17
 */
@Controller
@RequestMapping(value = "/app/mockexam/special/user/")
public class TmockexamSpecialUserController{

	@Autowired
	private TmockexamSpecialUserService tmockexamSpecialUserService;
	@Autowired
	private AppMockexamSpecialRecordService appMockexamSpecialRecordService;
	@RequestMapping(value="compute")
	public void compute(AppMockexamSpecialRecord entity,HttpServletRequest request,HttpServletResponse response){ 
		String specialId=entity.getSpecialId();
		if(StringUtils.isBlank(specialId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "主题标识");
			return;
		}
		String userId=entity.getUserId();
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		String type=entity.getType();
		if(StringUtils.isBlank(type)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "类型标识");
			return;
		}
		String userSpecialId=request.getParameter("userSpecialId");
    	if(StringUtils.isBlank(userSpecialId)){
    		AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "记录标识");
			return;
    	}
		List<AppMockexamSpecialRecord> findList = appMockexamSpecialRecordService.findList(entity);
		BigDecimal success=new BigDecimal(0);
    	for(AppMockexamSpecialRecord model:findList){
    		BigDecimal score = model.getScore();
    		success=success.add(score);
    	}
    	TmockexamSpecialUser tsUser=new TmockexamSpecialUser();
    	tsUser.setId(userSpecialId);
    	tsUser.setScore(success);
    	tsUser.setBarbecueEdtime(new Date());
    	tsUser.setState(4);
    	tmockexamSpecialUserService.update(tsUser);
    	JSONObject json=new JSONObject();
    	json.put("success", success);
    	AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "findPage")
	public void findPage(TmockexamSpecialUser tmockexamSpecialUser, HttpServletRequest request, HttpServletResponse response) {
		 String userId=request.getParameter("userId");
		 if(StringUtils.isBlank(userId)){
			 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			 return;
		 }
		 tmockexamSpecialUser.setCreateBy(userId);
		tmockexamSpecialUser.setPage(new Page<>(request, response));
		Page<TmockexamSpecialUser> page = tmockexamSpecialUserService.findPage(tmockexamSpecialUser);
	}

	

	/**
	 * 保存专项练习记录表
	 */
	@PostMapping(value = "save")
	public void save(TmockexamSpecialUser entity,HttpServletRequest request,HttpServletResponse response) {
		 String specialId=entity.getSpecialId();		// 专项练习标识
		 if(StringUtils.isBlank(specialId)){
			 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "专项练习标识");
			 return;
		 }
		 String userId=request.getParameter("userId");
		 if(StringUtils.isBlank(userId)){
			 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			 return;
		 }
		 String type=entity.getType();		// 类型说明
		 if(StringUtils.isBlank(type)){
			 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "数据类型");
			 return;
		 }
		 TmockexamSpecialUser model=new TmockexamSpecialUser();
		 model.setSpecialId(specialId);
		 model.setCreateBy(userId);
		 model.setType(type);
		 List<TmockexamSpecialUser> findList = tmockexamSpecialUserService.findList(model);
		 if(findList!=null&&findList.size()>0){
			entity = findList.get(0);
			JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(entity));
			AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
			return;
		 }
		 
		 Date barbecueTime=new Date();		// 练习时间
		 BigDecimal score=new BigDecimal(0.0);		// 得分
		 entity.setBarbecueTime(barbecueTime);
		 entity.setState(3);
		 entity.setScore(score);
		 entity.setCreateBy(userId);
		tmockexamSpecialUserService.save(entity);
		JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(entity));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
	}
	
	
	
}